package com.kayt.cyberBar.cpublic.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cpublic.business.dao.IPublicUserStatusDao;
import com.kayt.cyberBar.cpublic.business.entity.po.CPublicUserStatus;

@Repository("publicUserStatusDao")
public class PublicUserStatusDaoImpl implements IPublicUserStatusDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public PageData<CPublicUserStatus> getStatusList(LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<CPublicUserStatus> salePageData = this.entityDao.getPagingData(CPublicUserStatus.class, -1, -1, null, null, orderMap);
		return salePageData;
	}
	
	@Override
	public Boolean checkExist(String name) throws SQLException {
		String hql = "select count(o.id) as count from CPublicUserStatus o where o.userStatusName=?";
        List tempMapList;
        Boolean exist = false;
        tempMapList = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{name});
        if(tempMapList != null && !tempMapList.isEmpty()) {
            Map tempMap = (Map)tempMapList.get(0);
            Long tempCount = (Long)tempMap.get("count");
            if (tempCount > 0) {
            	exist = true;
            }
        }
		return exist;
	}
	
	@Override
	public CPublicUserStatus getStatusById (String statusId) {
		CPublicUserStatus status = null;
		if (statusId != null) {
			Long id;
			try {
				id = Long.parseLong(statusId);
			} catch (Exception e) {
				return null;
			}
			status = this.entityDao.findById(id, CPublicUserStatus.class);
		}
		return status;
	}

	@Override
	public Long addStatus (CPublicUserStatus status) throws SQLException {
		return this.entityDao.save(status);
	}
	
	@Override
	public void updateStatus (CPublicUserStatus status) throws SQLException {
		this.entityDao.update(status);
	}
	
	@Override
	public int deleteStatus (String statusId) {
		if (statusId != null) {
			Long id;
			try {
				id = Long.parseLong(statusId);
			} catch (Exception e) {
				return -1;
			}
			CPublicUserStatus userStatus = this.entityDao.findById(id, CPublicUserStatus.class);
			if (userStatus != null) {
				this.entityDao.deleteDetached(userStatus);
				return 0;
			}
		}
		return -1;
	}

	
}
